﻿Public Class CLSPedidos
    Dim sql As String
    Dim objbanco As New CLSBanco

    Dim M_Ped_ID As Integer
    Dim M_Ped_CID As Integer
    Dim M_Ped_Dat As Date
    Dim M_Ped_DV As Date
    Dim M_Ped_DP As Date
    Dim M_Ped_SP As String
    Dim M_Ped_VT As Single
    Dim novo As Boolean


    Public Property ID() As Integer
        Get
            Return M_Ped_ID
        End Get
        Set(ByVal value As Integer)
            M_Ped_ID = value
        End Set
    End Property

    Public Property ID_Cliente() As Integer
        Get
            Return M_Ped_CID
        End Get
        Set(ByVal value As Integer)
            M_Ped_CID = value
        End Set
    End Property

    Public Property Data_Pedido() As Date
        Get
            Return M_Ped_Dat
        End Get
        Set(ByVal value As Date)
            M_Ped_Dat = value
        End Set
    End Property
    Public Property Data_Vencimento() As Date
        Get
            Return M_Ped_DV
        End Get
        Set(ByVal value As Date)
            M_Ped_DV = value
        End Set
    End Property
    Public Property Data_Pagamento() As Date
        Get
            Return M_Ped_DP
        End Get
        Set(ByVal value As Date)
            M_Ped_DP = value
        End Set
    End Property
    Public Property Situação_Pedido() As String
        Get
            Return M_Ped_SP
        End Get
        Set(ByVal value As String)
            M_Ped_SP = value
        End Set
    End Property
    Public Property valorTotalPedido() As Single
        Get
            Return M_Ped_VT
        End Get
        Set(ByVal value As Single)
            M_Ped_VT = value
        End Set
    End Property


    Public Sub Gravar(ByVal novo As Boolean)
        Try
            If novo Then
                sql = "insert into Pedidos " & _
                  "(Ped_CID" & _
                  ",Ped_Dat" & _
                  ",Ped_DV" & _
                  ",Ped_DP" & _
                  ",Ped_SP" & _
                  ",Ped_VT" & _
                  ") values " & _
                  "('" & M_Ped_CID & "'" & _
                  ",'" & M_Ped_Dat & "'" & _
                  ",'" & M_Ped_DV & "'" & _
                  ",'" & M_Ped_DP & "'" & _
                  ",'" & M_Ped_SP & "'" & _
                  "," & M_Ped_VT & _
                  ")"
                objbanco.executar_comando(sql)
                ID = buscar_ultimo_codigo()
            Else
                sql = "update Pedidos set" & _
               " Ped_CID= '" & M_Ped_CID & "'" & _
               ",Ped_Dat= '" & M_Ped_Dat & "'" & _
               ",Ped_DV= '" & M_Ped_DV & "'" & _
               ",Ped_DP= '" & M_Ped_DP & "'" & _
               ",Ped_SP= '" & M_Ped_SP & "'" & _
               ",Ped_VT= " & M_Ped_VT & _
               " where Ped_ID=" & M_Ped_ID
                objbanco.executar_comando(sql)

            End If
        Catch ex As Exception
            MsgBox("Ocorreu o erro: " & ex.Message, MsgBoxStyle.Critical)
            objbanco.fechar_banco()
        End Try

    End Sub

    Public Function excluir(ByVal meucliente As Integer) As Boolean
        sql = "delete from Pedidos where Cli_ID=" & meucliente
        objbanco.executar_comando(sql)
        Return True
    End Function


    Public Function excluirCestaPedido(ByVal meucliente As Integer) As Boolean
        sql = "delete from PedidoCesta where id_itensped=" & meucliente
        objbanco.executar_comando(sql)
        Return True
    End Function

    Public Function LocalizarPorID(ByVal digiteID As Integer) As DataTable
        sql = "select * from Pedidos where Cli_ID = " & digiteID
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objda.Fill(objds)
        Return objds.Tables(0)
    End Function


    Public Function LocalizarCodigoPedido(ByVal digiteID As Integer) As DataTable
        sql = "select * from Pedidos where Ped_ID = " & digiteID
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objda.Fill(objds)
        Return objds.Tables(0)
    End Function


    Public Function LocalizarPorNome(ByVal NomeCesta As String) As DataTable
        sql = "select * from Cestas where Ces_Nom like '" & NomeCesta & "%'"
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objda.Fill(objds)
        Return objds.Tables(0)
    End Function
    Public Function buscar_ultimo_codigo() As Integer
        sql = "select max(Ped_ID) from pedidos"
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objds.Tables.Clear()
        objda.Fill(objds)
        Return objds.Tables(0).Rows(0).Item(0)

    End Function

    Public Function mostrarPedido(ByVal conteudo As String) As DataTable
        sql = "select Cli_id, cli_Nom,Cli_CPF, ped_id, ped_dat, ped_dv, ped_SP, ped_vt, pc_idCesta, ces_nom from cliente , pedidos , pedidoCesta , cestas where pedidos.ped_Cid = Cliente.Cli_id and pedidos.ped_id = pedidoCesta.Pc_idpedido and cestas.ces_idcesta = pedidocesta.pc_idcesta and  ped_id = " & conteudo
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objds.Tables.Clear()
        objda.Fill(objds)
        Return objds.Tables(0)
    End Function

    Public Function mostrarPedidoCliente(ByVal conteudo As String) As DataTable
        sql = "select id_itensped as Codigo, ces_nom as [Nome Cesta],qtd as [Quantidade],valor as [Valor]  from  cliente , pedidos , pedidoCesta , cestas where pedidos.ped_Cid = Cliente.Cli_id and pedidos.ped_id = pedidoCesta.Pc_idpedido and cestas.ces_idcesta = pedidocesta.pc_idcesta and  ped_id = " & conteudo
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objds.Tables.Clear()
        objda.Fill(objds)
        Return objds.Tables(0)
    End Function

    Public Function TotalPedido(ByVal conteudo As String) As DataTable
        sql = "Select Sum(PedidoCesta.valor) as [Receita Total] from PedidoCesta where PedidoCesta.PC_idPedido = " & conteudo
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objds.Tables.Clear()
        objda.Fill(objds)
        Return objds.Tables(0)
    End Function

    Public Function LocalizarPordata(ByVal Conteudo As String) As DataTable
        sql = "SELECT Client.Cli_Nom, pedidos.ped_dat, ped_DV, ped_SP FROM pedidos, cliente WHERE ped_Cid = Cli_id and Ped_SP = '" & Conteudo & "'"
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet

        objda.Fill(objds)
        Return objds.Tables(0)
    End Function


    Public Function buscarCesta(ByVal Conteudo As String) As DataTable
        sql = "SELECT Cliente.Cli_Nom, pedidos.ped_dat, ped_DV, ped_SP FROM pedidos, cliente WHERE ped_Cid = Cli_id and Ped_SP = '" & Conteudo & "'"
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet

        objda.Fill(objds)
        Return objds.Tables(0)
    End Function
End Class
